﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using DevExpress.Data;
using DevExpress.Data.PivotGrid;
using DevExpress.Utils;
using DevExpress.Web.Mvc;
using DevExpress.XtraPivotGrid;
using DistanceLearning.Models;

namespace DistanceLearning.Controllers
{
    public class InformationController : JournalBaseController
    {
        public ActionResult Index()
        {
            switch (UserKind)
            {
                case UserKind.Parent:
                    return View("ParentInformation", Parent);
                case UserKind.Teacher:
                    return View("TeacherInformation", Teacher);
            }

            return View();
        }

        public PartialViewResult ParentInformation()
        {
            return PartialView();
        }

        public PartialViewResult MarksPagePartial()
        {
            return PartialView();
        }

        public PartialViewResult TeacherInformation()
        {
            return PartialView();
        }

        public PartialViewResult PupilInformation()
        {
            return PartialView();
        }

        private PivotGridSettings _marksPivotGridSettings;
        public PivotGridSettings MarksPivotGridSettings
        {
            get
            {
                if (_marksPivotGridSettings == null)
                    _marksPivotGridSettings = CreatePivotGridSettings();
                return _marksPivotGridSettings;
            }
        }

        public PivotGridSettings CreatePivotGridSettings()
        {
            PivotGridSettings settings = new PivotGridSettings();

            settings.Name = "journalPivotGrid";
            settings.CallbackRouteValues = new { Controller = "Information", Action = "MarksPagePartial" };
            //settings.CustomActionRouteValues = new { Controller = "Journal", Action = "CustomActionSampleReportsPartial" };
            settings.Width = Unit.Percentage(100);
            settings.OptionsView.ShowHorizontalScrollBar = true;

            settings.CustomGroupInterval += OnCustomGroupInterval;
            settings.CustomUnboundFieldData += OnCustomUnboundFieldData;
            settings.CustomCellValue += OnCustomCellValue;
            settings.ClientSideEvents.BeginCallback = "OnJournalGridBeginCallback";
            settings.Styles.CellStyle.Cursor = "pointer";
            settings.OptionsPager.RowsPerPage = 50;


            settings.Fields.Add(field =>
            {
                field.Area = PivotArea.RowArea;
                field.AreaIndex = 0;
                field.UnboundExpression = "Concat([Pupil.UserDetails.LastName], ' ', [Pupil.UserDetails.FirstName], ' ', [Pupil.UserDetails.PatronymicName])";
                field.UnboundType = UnboundColumnType.String;
                field.Caption = "Имя ученика";
            });
            settings.Fields.Add(field =>
            {
                field.Area = PivotArea.RowArea;
                field.AreaIndex = 1;
                field.FieldName = "ConcreteLesson.JournalLesson.SubjectID";
                field.Caption = "Предмет";
            });
            settings.Fields.Add(field =>
            {
                field.Area = PivotArea.ColumnArea;
                field.AreaIndex = 0;
                field.FieldName = "ConcreteLesson.Date";
                field.Caption = "Четверть";
                field.GroupInterval = PivotGroupInterval.Custom;
                field.SummaryType = PivotSummaryType.Average;
                field.GrandTotalText = "Среднее";
            });
            settings.Fields.Add(field =>
            {
                field.Area = PivotArea.ColumnArea;
                field.AreaIndex = 1;
                field.FieldName = "ConcreteLesson.Date";
                field.Caption = "Месяц";
                field.GroupInterval = PivotGroupInterval.DateMonth;
                field.TotalsVisibility = PivotTotalsVisibility.None;
            });
            settings.Fields.Add(field =>
            {
                field.Area = PivotArea.ColumnArea;
                field.AreaIndex = 2;
                field.FieldName = "ConcreteLesson.Date";
                field.Caption = "Дата";
                field.GroupInterval = PivotGroupInterval.Date;
                field.ValueFormat.FormatType = FormatType.DateTime;
                field.ValueFormat.FormatString = "dd";
            });
            settings.Fields.Add(field =>
            {
                field.Visible = true;
                field.Area = PivotArea.DataArea;
                field.AreaIndex = 0;
                field.Caption = "Оценка";
                field.UnboundType = UnboundColumnType.Object;
                field.EmptyCellText = string.Empty;
                field.EmptyValueText = string.Empty;
                field.FieldName = "Mark1";
                field.SummaryType = PivotSummaryType.Average;
                //field.UnboundExpression
                //field.UnboundType = UnboundColumnType.String;
                field.CellFormat.FormatString = "{0:0.##}";
                field.CellFormat.FormatType = FormatType.Numeric;

            });

            //settings.PreRender = (sender, e) => {
            //    UpdatePivotGridLayout((MVCxPivotGrid)sender);
            //};
            //settings.BeforeGetCallbackResult = (sender, e) => {
            //    if (ViewBag.IsReloadLayout != null && ViewBag.IsReloadLayout)
            //        UpdatePivotGridLayout((MVCxPivotGrid)sender);
            //};

            return settings;
        }
    }
}
